package org.wang.generalcode.modules.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wang.generalcode.modules.sys.pojo.entity.Role;
import org.wang.generalcode.modules.sys.pojo.model.Option;
import org.wang.generalcode.modules.sys.pojo.params.RolePageParams;
import org.wang.generalcode.modules.sys.pojo.params.RoleParams;
import org.wang.generalcode.modules.sys.pojo.vo.RoleVO;
import org.wang.generalcode.modules.sys.service.IRoleService;
import org.wang.generalcode.support.mybaties.Condition;
import org.wang.generalcode.support.mybaties.Query;
import org.wang.generalcode.support.result.Result;
import org.wang.generalcode.support.utils.Func;

import javax.validation.Valid;
import java.util.List;

/**
 * 角色表 控制器
 *
 * @author wz
 * @since 2024-08-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Schema(description = "角色表", name = "角色表接口")
public class RoleController {

	private final IRoleService roleService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Schema(description = "详情", example = "传入role")
	public Result<RoleVO> detail(Role role) {
		RoleVO roleVO =  new RoleVO();
		Role detail = roleService.getOne(Condition.getQueryWrapper(role));
		BeanUtil.copyProperties(detail, roleVO);
		return Result.success(roleVO);
	}

	/**
	 * 分页 角色表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Schema(description = "分页", example = "传入role")
	public Result<IPage<Role>> list(Role role, Query query) {
		IPage<Role> pages = roleService.page(Condition.getPage(query), Condition.getQueryWrapper(role));
		return Result.success(pages);
	}

	/**
	 * 自定义分页 角色表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@Schema(description = "分页", example = "传入role")
	public Result<IPage<RoleVO>> page(RolePageParams queryParams, Query query) {
		IPage<RoleVO> pages = roleService.selectRolePage(Condition.getPage(query), queryParams);
		return Result.success(pages);
	}

	/**
	 * 新增 角色表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@Schema(description = "新增", example = "传入role")
	public Result save(@Valid @RequestBody RoleParams roleParams) {
		return Result.judge(roleService.saveRole(roleParams));
	}

	/**
	 * 修改 角色表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@Schema(description = "修改", example = "传入role")
	public Result update(@Valid @RequestBody Role role) {
		return Result.judge(roleService.updateById(role));
	}


	/**
	 * 删除 角色表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@Schema(description = "删除", example = "传入ids")
	public Result remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
		return Result.judge(roleService.removeByIds(Func.toLongList(ids)));
	}


	/**
	 * 角色下拉列表
	 */
	@GetMapping("/options")
	@ApiOperationSupport(order = 9)
	@Schema(description = "分页", example = "传入role")
	public Result<List<Option<Long>>> listRoleOptions() {
		List<Option<Long>> list = roleService.listRoleOptions();
		return Result.success(list);
	}

	/**
	 * 获取角色的菜单ID集合
	 */
	@GetMapping("/menuIds")
	@ApiOperationSupport(order = 9)
	@Schema(description = "获取角色的菜单ID集合", example = "传入role")
	public Result<List<Long>> getRoleMenuIds(@Parameter(description = "角色ID") @RequestParam Long roleId) {
		List<Long> menuIds = roleService.getRoleMenuIds(roleId);
		return Result.success(menuIds);
	}


	@Operation(summary = "分配菜单(包括按钮权限)给角色")
	@ApiOperationSupport(order = 10)
	@PutMapping("/menus")
	public Result<?> assignMenusToRole(@RequestParam Long roleId, @RequestBody List<Long> menuIds) {
		boolean result = roleService.assignMenusToRole(roleId, menuIds);
		return Result.judge(result);
	}

}
